#pragma once
#include<iostream>
#include"Protocal.hpp"


class ServerCal
{
public:
    ServerCal()
    {}
    Response CalculatorHelper(const Request &req)
    {
        Response resp(0, 0);
        switch(req._op)
        {
            case '+':
                resp._result = req._x + req._y;
                break;
            case '-':
                resp._result = req._x - req._y;
                break;
            case '*':
                resp._result = req._x * req._y;
                break;
            case '/':
            {
                if(req._y == 0) resp._code = 1;
                else resp._result = req._x / req._y;
                break;
            }

            case '%':
            {
                if(req._y == 0) resp._code = 1;
                else resp._result = req._x % req._y;   
                break;
            }
            default :
                resp._code = 3;
        }
        return resp;
    }
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if(!r) return "";
        Request req;
        r = req.Deserialize(content);
        if(!r) return "";

        content = "";
        Response resp = CalculatorHelper(req);
        resp.Serialize(&content);
        content = Encode(content);

        return content;
    }

    ~ServerCal()
    {}

};